# _*_ encoding: utf-8 _*_
# __author__ = 'lx'
import os
import shutil

def read_last_line(file):
    """读取log文件的最后一行，返回文件夹名的序号"""
    with open(file, 'w+') as fp:
        lines = fp.readlines()
        if lines:
            last_line = lines[-1]
            num = int(last_line) + 1
            name = str(num).rjust(4, '0')
        else:
            name = '0001'
    return name

def write_last_line(file, name):
    """在log里写入文件夹名的序号"""
    with open(file, 'w+') as fp:
        fp.write(str(name))

def copy_dir(path, mv_path):
    """复制目录及其子目录和所有文件"""
    if not os.path.exists(mv_path):
        os.mkdir(mv_path)
    for item in os.listdir(path):
        item_path = os.path.join(path,item)
        if os.path.isdir(item_path):
            new_dir = os.path.join(mv_path, item)
            if not os.path.exists(new_dir):
                os.mkdir(new_dir)
            copy_dir(item_path, new_dir)
        elif os.path.isfile(item_path):
            shutil.copy(item_path, mv_path)